﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_DTO.RequestModels.Material;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace Ekton_Manage_Web.Controllers
{
    /// <summary>
    /// 材料
    /// </summary>
    /// 
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize("Permission")]
    public class MaterialController : ControllerBase
    {
 
        private readonly ISelectMaterialList _selectMaterialList;

        public MaterialController(ISelectMaterialList selectMaterialList)
        {
        
            _selectMaterialList = selectMaterialList;
        }

        /// <summary>
        /// 查询全部材料列表
        /// </summary>
        /// 
        [HttpGet]
        public async Task<MessageModel<PageModel<material>>> GetMeterialList([FromQuery] QueryDataParame query)
        {
            var result = await _selectMaterialList.GetMeterialList(query);
            return new MessageModel<PageModel<material>>
            {
                msg = "数据获取成功",
                response = new PageModel<material>
                {
                    pageIndex = query.pageIndex,
                    pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(result.Item2) / query.pageSize)),
                    dataCount = result.Item2,
                    pageSize = query.pageSize,
                    data = result.Item1
                }
            };
        }

        /// <summary>
        /// 编辑材料信息
        /// </summary>
        /// 
        [HttpPut]
        public async Task<Message> PutMeterialInfo([FromBody] DtoSelectMaterialList query)
        {
       
            return await _selectMaterialList.PutMeterialList(query);
        }

        /// <summary>
        /// 添加材料信息
        /// </summary>
        /// 
        [HttpPut]
        public async Task<Message> PostMeterialInfo([FromBody] DtoSelectMaterialList query)
        {
            return await _selectMaterialList.PostMeterialList(query);
        }

        /// <summary>
        /// 删除材料信息
        /// </summary>
        /// 
        [HttpDelete("{id}")]
        public async Task<Message> DeleteMeterialInfo(int id)
        {
            return await _selectMaterialList.DeleteMeterialInfo(id);
        }
    }
}
